package com.example.broad;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import com.example.broad.receiver.BroadReciverFirst;
import com.example.broad.receiver.BroadReciverSecond;
import com.example.broad.receiver.StandardReceiver;

/**
  *
  * @description 有序广播
  * @param 
  * @return 
  * author snjgm
  * time 2022/7/22 下午5:27
  */
public class BroadOrderActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String ORDER_ACTION = "broadOder";

    private BroadReciverFirst broadReciverFirst;
    private BroadReciverSecond broadReciverSecond;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_order);
        findViewById(R.id.btn_sendBroadOrder).setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //注册第一个广播接收器
        broadReciverFirst = new BroadReciverFirst();
        IntentFilter firstFilter = new IntentFilter(ORDER_ACTION);
        //設置廣播优先级别
        firstFilter.setPriority(9);
        registerReceiver(broadReciverFirst, firstFilter);
        //注册第二个广播接收器
        broadReciverSecond = new BroadReciverSecond();
        IntentFilter secondFilter = new IntentFilter(ORDER_ACTION);
        secondFilter.setPriority(10);
        registerReceiver(broadReciverSecond, secondFilter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(broadReciverFirst);
        unregisterReceiver(broadReciverSecond);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_sendBroadOrder:
                Intent intent = new Intent(ORDER_ACTION);
                sendOrderedBroadcast(intent,null);
                break;
        }
    }
}